package com.coocaa.launcher.statusplugins.externalstorege;

import com.coocaa.launcher.framework.launcherhost.statusbar.StatusBarPlugin;
import com.coocaa.launcher.framework.launcherhost.statusbar.StatusBarPluginView;
import com.coocaa.launcher.statusplugins.externalstorege.StatusPluginExternalStorageView.ESTORAGETYPE;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.framework.utils.Storage;
import com.coocaa.x.framework.utils.Storage.IStorageListener;
import com.coocaa.x.framework.utils.Storage.SDevice;

import java.util.List;

public class StatusPluginExternalStorage extends StatusBarPlugin implements IStorageListener {
    public interface ExternalStorageChange {
        public void onChange(ESTORAGETYPE type, boolean bIn);
    }

    public static StatusPluginExternalStorage instance = new StatusPluginExternalStorage();
    private ExternalStorageChange iExternalStorageChange = null;
    private StatusBarPluginView view = null;

    public StatusPluginExternalStorage() {
        super("statusplugin.exteralstorage");
    }

    @Override
    public StatusBarPluginView inflate(String pattern) {
        if (view == null)
            view = new StatusPluginExternalStorageView(mContext, getName());
        return view;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        Storage.addListener(this);
        iExternalStorageChange = (ExternalStorageChange) view;
        List<SDevice> devices = Storage.getSDeviceList();
        for (SDevice device : devices) {
            LogUtils.d("Storage", "type:" + device.getMediaType().toString() + "sunny  onCreate  " + device.getMountPoint() + "; " + device.isMounted());
            if (!device.isMounted())
                continue;
            if (device.getMediaType() == SDevice.MEDIA_TYPE.DISK)
                iExternalStorageChange.onChange(ESTORAGETYPE.USB, true);
            else if (device.getMediaType() == SDevice.MEDIA_TYPE.SDCARD && device.isExternal())
                iExternalStorageChange.onChange(ESTORAGETYPE.SDCARD, true);
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Storage.removeListener(this);
        view = null;
        iExternalStorageChange = null;
    }

    @Override
    public void onStorageMounted(SDevice device) {
        LogUtils.d("Storage", "sunny type:" + device.getMediaType().toString() + " isMounted; isEmulate:" + device.isExternal());
        if (device.getMediaType() == SDevice.MEDIA_TYPE.DISK)
            iExternalStorageChange.onChange(ESTORAGETYPE.USB, true);
        else if (device.getMediaType() == SDevice.MEDIA_TYPE.SDCARD && device.isExternal())
            iExternalStorageChange.onChange(ESTORAGETYPE.SDCARD, true);
    }

    @Override
    public void onStorageRemoved(SDevice device) {
        LogUtils.d("Storage", "type:" + device.getMediaType().toString() + "sunny  onStorageRemoved  ");
        if (device.getMediaType() == SDevice.MEDIA_TYPE.DISK && getIfDevice(SDevice.MEDIA_TYPE.DISK))
            iExternalStorageChange.onChange(ESTORAGETYPE.USB, false);
        else if (device.getMediaType() == SDevice.MEDIA_TYPE.SDCARD && device.isExternal() && getIfDevice(SDevice.MEDIA_TYPE.SDCARD))
            iExternalStorageChange.onChange(ESTORAGETYPE.SDCARD, false);
    }

    private boolean getIfDevice(SDevice.MEDIA_TYPE type) {
        boolean NoDevice = true;
        List<SDevice> devices = Storage.getSDeviceList();
        for (SDevice device : devices) {
            LogUtils.d("", "sunny getIfDevice:" + device.getMountPoint() + ";" + device.getMediaType() + "; " + type);
            if (!device.isMounted())
                continue;
            if (device.getMediaType().toString().equals(type.toString()) && device.isExternal()) {
                NoDevice = false;
            }
        }
        return NoDevice;
    }
}
